﻿using DevLib.TestCase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/** 
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 * Description: 
 * Author: Heller (HellerSong@outlook.com)
 * CreationTime: 3/17/2015 8:25:42 AM
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
 **/
namespace DevLib.FileTool
{
    public class TextEditor
    {
		/* Rule: 
		 *		A one-time operation for static method;
		 *		A frequence operation for instance method with specified TEXT file;
		 */
        private String txtFile;
        private static Encoding myEncoding = Encoding.UTF8;
        public static int itemFormatLength = 20;

        public TextEditor(String txtFile)
        {
            StrCheck.CheckChainFileOperable(txtFile);

            this.txtFile = txtFile;
        }


        public static String[] ReadAllLines(String txtFile)
        {
            StrCheck.CheckChainFileOperable(txtFile);

            return File.ReadAllLines(txtFile);
        }
        public static String ReadAll(String txtFile)
        {
            StrCheck.CheckChainFileOperable(txtFile);

            return File.ReadAllText(txtFile);
        }
        public static void WriteAllLines(String txtFile, String[] values)
        {
            // if not existed create,
            // values should not null, length can be 0
            StrCheck.CheckChainArgUsable(txtFile);
			StrCheck.CheckChainArgNotNullArray(values);

            File.WriteAllLines(txtFile, values);
        }
        public static void WriteAll(String txtFile, String content)
        {
            StrCheck.CheckChainArgUsable(txtFile);
            StrCheck.CheckChainArgNotNull(content);

            File.WriteAllText(txtFile, content);
        }
        public static void TrimAllBlankLines(String txtFile)
        {
            StrCheck.CheckChainFileOperable(txtFile);

            // If the file is DataResource file, not allow blank lines
            List<String> strList = new List<String>();
            String strLine;

            using (FileStream fs = new FileStream(txtFile, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs, myEncoding))
                {
                    strLine = sr.ReadLine();
                    while (strLine != null)
                    {
                        if (strLine.Length > 0)
                            strList.Add(strLine);
                        strLine = sr.ReadLine();
                    }
                    sr.Close();
                }
            }
            WriteAllLines(txtFile, strList.ToArray());
        }
        public static void CleanContent(String txtFile)
        {
            StrCheck.CheckChainFileOperable(txtFile);

            File.WriteAllLines(txtFile, new String[] { });
        }


        public void CleanContent()
        {
            CleanContent(txtFile);
        }
        public String ReadFirstLine()
        {
            String strLine = null;

            using (FileStream fs = new FileStream(txtFile, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs, myEncoding))
                {
                    strLine = sr.ReadLine();
                    sr.Close();
                }
            }

            return strLine;
        }
        public String[] PickUpLinesContainSubString(String subString)
        {
            List<String> strList = new List<String>();

            return strList.ToArray();
        }

        public void WriteLine()
        {
            using (FileStream fs = new FileStream(txtFile, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fs, myEncoding))
                {
                    sw.WriteLine();
                    sw.Close();
                }
            }
        }
        public void WriteLine<T>(T value)
        {
            using (FileStream fs = new FileStream(txtFile, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fs, myEncoding))
                {
                    sw.WriteLine(value.ToString());
                    sw.Close();
                }
            }
        }
        public void Write<T>(T value)
        {
            using (FileStream fs = new FileStream(txtFile, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fs, myEncoding))
                {
                    sw.Write(value.ToString());
                    sw.Close();
                }
            }
        }
        public void WriteItem(String itemKey, String itemValue)
        {
            using (FileStream fs = new FileStream(txtFile, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fs, myEncoding))
                {
                    sw.WriteLine(itemKey.PadLeft(itemFormatLength) + ": " + itemValue);
                    sw.Close();
                }
            }
        }
        public void WriteItem(String itemKey, int itemValue)
        {
            using (FileStream fs = new FileStream(txtFile, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fs, myEncoding))
                {
                    sw.WriteLine(itemKey.PadLeft(itemFormatLength) + ": " + itemValue);
                    sw.Close();
                }
            }
        }
        public void writeObject<T>(T value)
        {
            // Save object data as key value split by "="

        }

        public void WriteSeperatorLine(int seperatorCount)
        {
            using (FileStream fs = new FileStream(txtFile, FileMode.Append))
            {
                using (StreamWriter sw = new StreamWriter(fs, myEncoding))
                {
                    for (int i = 0; i < seperatorCount + 2; i++)
                        sw.Write("-");
                    sw.WriteLine();
                    sw.Close();
                }
            }
        }


    }
}
